package com.headingsoft.jxc.domain;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.EntityManager;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.TypedQuery;

import org.springframework.roo.addon.entity.RooEntity;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.tostring.RooToString;

@RooJavaBean
@RooToString
@RooEntity(finders = "findGoodsCategorysByParent")
public class GoodsCategory {

    private String name;

    private int orderIndex;

    @ManyToOne
    private GoodsCategory parent;

    @OneToMany(cascade = CascadeType.ALL, mappedBy="parent")
    @OrderBy("orderIndex")
    private List<GoodsCategory> children = new ArrayList<GoodsCategory>();
    
    public static List<GoodsCategory> findAllTopGoodsCategory() {
        EntityManager em = GoodsCategory.entityManager();
        TypedQuery<GoodsCategory> q = em.createQuery("SELECT GoodsCategory FROM GoodsCategory AS goodscategory WHERE goodscategory.parent IS NULL", GoodsCategory.class);
        return q.getResultList();
    }
}
